/*
 * Copyright (C) 2010-2011 Comcast Cable.  All Rights Reserved.
 */
package com.hercules.android.receiver.model.events;

import java.util.Map;

/**
 * <p>This is a subclass of XREEvent that represents the "onConnect" event. It defines some properties specific to this
 * event. Note that values of all custom properties are actually stored in the XREEvent#params map.</p>
 *
 * <p><strong>Thread safety:</strong> This class is mutable and not thread safe.</p>
 *
 * @author saarixx, jmn
 * @version 1.0
 */
public class XREConnectEvent extends XREEvent {

    /**
     * <p>Represents the name of the "on connect" event.</p>
     *
     * <p>This is a string constant used by the user when setting the name of the event.</p>
     */
    public static final String ON_CONNECT_EVENT_NAME = "onConnect";

    /**
     * <p>Represents the name used to retrieve the device caps parameter.</p>
     */
    private static final String DEVICE_CAPS = "deviceCaps";

    /**
     * <p>Represents the name used to retrieve the application name parameter.</p>
     */
    private static final String APPLICATION_NAME = "applicationName";

    /**
     * <p>Represents the name used to retrieve the minimum version parameter.</p>
     */
    private static final String MINIMUM_VERSION = "minimumVersion";

    /**
     * <p>Represents the name used to retrieve the authentication token parameter.</p>
     */
    private static final String AUTHENTICATION_TOKEN = "authenticationToken";

    /**
     * <p>Represents the name used to retrieve the session GUID parameter.</p>
     */
    private static final String SESSION_GUID = "sessionGUID";

    /**
     * <p>Represents the name used to retrieve the application params parameter.</p>
     */
    private static final String APP_PARAMS = "appParams";

    /**
     * <p>Represents the name used to retrieve the current command index parameter.</p>
     */
    private static final String CURRENT_COMMAND_INDEX = "currentCommandIndex";

    /**
     * <p>Represents the name used to retrieve the reconnect parameter.</p>
     */
    private static final String RECONNECT = "reconnect";

    /**
     * <p>Creates new instance of {@link XREConnectEvent} class.</p>
     */
    public XREConnectEvent() {
        // empty constructor
    }

    /**
     * <p>Retrieves the device capabilities.</p>
     *
     * @return the device capabilities
     *
     * @throws IllegalStateException if the requested parameter has wrong type in the parameters map or the params is
     *                               null
     */
    public XREDeviceCapabilities getDeviceCaps() {
        return getTypedParam(DEVICE_CAPS, XREDeviceCapabilities.class);
    }

    /**
     * <p>Sets the device capabilities.</p>
     *
     * @param deviceCaps the device capabilities
     *
     * @throws IllegalStateException if params is null
     */
    public void setDeviceCaps(XREDeviceCapabilities deviceCaps) {
        setParam(DEVICE_CAPS, deviceCaps);
    }

    /**
     * <p>Retrieves the name of the XRE application that the receiver wishes to load.</p>
     *
     * @return the name of the XRE application that the receiver wishes to load
     *
     * @throws IllegalStateException if the requested parameter has wrong type in the parameters map or the params is
     *                               null
     */
    public String getApplicationName() {
        return getTypedParam(APPLICATION_NAME, String.class);
    }

    /**
     * <p>Sets the name of the XRE application that the receiver wishes to load.</p>
     *
     * @param applicationName the name of the XRE application that the receiver wishes to load
     *
     * @throws IllegalStateException if params is null
     */
    public void setApplicationName(String applicationName) {
        setParam(APPLICATION_NAME, applicationName);
    }

    /**
     * <p>Retrieves the minimum version the application that the client can run.</p>
     *
     * @return the minimum version the application that the client can run
     *
     * @throws IllegalStateException if the requested parameter has wrong type in the parameters map or the params is
     *                               null
     */
    public String getMinimumVersion() {
        return getTypedParam(MINIMUM_VERSION, String.class);
    }

    /**
     * <p>Sets the minimum version the application that the client can run.</p>
     *
     * @param minimumVersion the minimum version the application that the client can run
     *
     * @throws IllegalStateException if params is null
     */
    public void setMinimumVersion(String minimumVersion) {
        setParam(MINIMUM_VERSION, minimumVersion);
    }

    /**
     * <p>Retrieves the string that allows a host to authenticate a receiver.</p>
     *
     * @return the string that allows a host to authenticate a receiver
     *
     * @throws IllegalStateException if the requested parameter has wrong type in the parameters map or the params is
     *                               null
     */
    public String getAuthenticationToken() {
        return getTypedParam(AUTHENTICATION_TOKEN, String.class);
    }

    /**
     * <p>Sets the string that allows a host to authenticate a receiver.</p>
     *
     * @param authenticationToken the string that allows a host to authenticate a receiver
     *
     * @throws IllegalStateException if params is null
     */
    public void setAuthenticationToken(String authenticationToken) {
        setParam(AUTHENTICATION_TOKEN, authenticationToken);
    }

    /**
     * <p>Retrieves the host-defined session GUID that was included in the NEW command (if this application was created
     * as a child of another application).</p>
     *
     * @return the host-defined session GUID that was included in the NEW command
     *
     * @throws IllegalStateException if the requested parameter has wrong type in the parameters map or the params is
     *                               null
     */
    public String getSessionGUID() {
        return getTypedParam(SESSION_GUID, String.class);
    }

    /**
     * <p>Sets the host-defined session GUID that was included in the NEW command (if this application was created as a
     * child of another application).</p>
     *
     * @param sessionGUID the host-defined session GUID that was included in the NEW command
     *
     * @throws IllegalStateException if params is null
     */
    public void setSessionGUID(String sessionGUID) {
        setParam(SESSION_GUID, sessionGUID);
    }

    /**
     * <p>Retrieves the application-defined set of parameters to pass to the application's initialization routine.</p>
     *
     * @return the application-defined set of parameters to pass to the application's initialization routine
     *
     * @throws IllegalStateException if the requested parameter has wrong type in the parameters map or the params is
     *                               null
     */
    @SuppressWarnings("unchecked")
    public Map<String, Object> getAppParams() {
        return getTypedParam(APP_PARAMS, Map.class);
    }

    /**
     * <p>Sets the application-defined set of parameters to pass to the application's initialization routine.</p>
     *
     * @param appParams the application-defined set of parameters to pass to the application's initialization routine
     *
     * @throws IllegalStateException if params is null
     */
    public void setAppParams(Map<String, Object> appParams) {
        setParam(APP_PARAMS, appParams);
    }

    /**
     * <p>Retrieves the last command index the receiver processed before the connection was lost. Only relevant during a
     * reconnection attempt.</p>
     *
     * @return the last command index the receiver processed before the connection was lost
     *
     * @throws IllegalStateException if the requested parameter has wrong type in the parameters map or the params is
     *                               null
     */
    @SuppressWarnings("boxing")
    public int getCurrentCommandIndex() {
        return getTypedParam(CURRENT_COMMAND_INDEX, Integer.class);
    }

    /**
     * <p>Sets the last command index the receiver processed before the connection was lost. Only relevant during a
     * reconnection attempt.</p>
     *
     * @param currentCommandIndex the last command index the receiver processed before the connection was lost
     *
     * @throws IllegalStateException if params is null
     */
    @SuppressWarnings("boxing")
    public void setCurrentCommandIndex(int currentCommandIndex) {
        setParam(CURRENT_COMMAND_INDEX, currentCommandIndex);
    }

    /**
     * <p>Retrieves the value that indicates whether this event represents a reconnection attempt or an initial
     * connection attempt.</p>
     *
     * @return the value that indicates whether this event represents a reconnection attempt or an initial connection
     *         attempt
     *
     * @throws IllegalStateException if the requested parameter has wrong type in the parameters map or the params is
     *                               null
     */
    @SuppressWarnings("boxing")
    public boolean isReconnect() {
        return getTypedParam(RECONNECT, Boolean.class);
    }

    /**
     * <p>Sets the value that indicates whether this event represents a reconnection attempt or an initial connection
     * attempt.</p>
     *
     * @param reconnect the value that indicates whether this event represents a reconnection attempt or an initial
     *                  connection attempt
     *
     * @throws IllegalStateException if params is null
     */
    @SuppressWarnings("boxing")
    public void setReconnect(boolean reconnect) {
        setParam(RECONNECT, reconnect);
    }
}
